package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import server.Server;
import transfer.StringData;
import util.Utils;
import widget.MenuWidget;

public class ChessOnlineView extends JPanel implements IChessOnlineView, ActionListener, MouseListener{
	private static final long serialVersionUID = 4823816098502814709L;
	public Server mMainScreen;
	private Image mBackground = null;
	public MenuBar mbar;
	public static DefaultTableModel dftMolde;
	public JTable jtable;
	public JPanel pnUserInfo;
	public JButton btnNew, btnUpdate, btnDelete;
	public String dateFormat = "dd-MM-yyyy";
	
	public ChessOnlineView(Server mainScreen, String x){
		this.mMainScreen = mainScreen;
	}
	
	public ChessOnlineView(Server mainScreen) {
		mbar = new MenuBar();
		this.setLayout(new BorderLayout());
		this.mMainScreen = mainScreen;
		MenuWidget menu = new MenuWidget(this);
		mainScreen.frame.setMenuBar(menu.getMenuBar());
		addUserInfo();
		addControl();
		addEvents();
		getAllDataBase();
		getAll();
	}
	
	@Override
	public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("Exit")){
        	int chooseAnswer = JOptionPane.showConfirmDialog(null, "Do you really want to exit?", "Confirm", JOptionPane.YES_NO_OPTION);
    		if (chooseAnswer == JOptionPane.YES_OPTION){
    			System.exit(0);
    		}	
        }
        
        if(e.getActionCommand().equals("Disconnect"))
        	mMainScreen.sendObject(new StringData("Disconnect", null));
        
        if(e.getActionCommand().equals("Log Out")){
        	Utils.removeUserSession();
        	mMainScreen.showHomePageView();
        }
        
        if(e.getActionCommand().equals("Refresh"))
        	getAll();
        
        if (e.getActionCommand().equals("League"))
        	mMainScreen.showLeagueManageView();
        
        if (e.getActionCommand().equals("User Account"))
        	mMainScreen.showUserAccountView();
        
        Object obj = e.getSource();
        
        if(obj.equals(btnNew)){
			doAdd();
		}			
		else if(obj.equals(btnUpdate)){
			doSave();
		}			
		else if(obj.equals(btnDelete)){				
			doDelete();
		}
    }	

	@Override
	public void onShow() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onHide() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(mBackground, 0, 0, null);
	}

	@Override
	public void lockText(boolean status) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doAdd() {
		if (btnNew.getText().equalsIgnoreCase("New")) {
			lockText(false);
			btnUpdate.setEnabled(true);
			btnNew.setText("Cancel");
		}else {
			lockText(true);
			btnUpdate.setEnabled(false);
			btnNew.setText("New");
			btnUpdate.setText("Save");
		}
	}

	@Override
	public void doSave() {
		getAll();
		clearText();
		lockText(true);
		btnUpdate.setEnabled(false);
		btnNew.setText("New");
		btnUpdate.setText("Save");
	}
	
	@Override
	public void addUserInfo(){
		String uname = "";
		if(mMainScreen.getAcc() != null)
			uname = mMainScreen.getAcc().getName();
		pnUserInfo = new JPanel();
		JLabel lblUsername = new JLabel("Hi, " + uname);
		lblUsername.setForeground(Color.BLUE);
		pnUserInfo.add(lblUsername);
	}

	@Override
	public void addControl() {
		
	}

	@Override
	public void getAll() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getAllDataBase() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addEvents() {
		btnNew.addActionListener(this);
		btnUpdate.addActionListener(this);
		btnDelete.addActionListener(this);
	}

	@Override
	public void clearText() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doDelete() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
